import 'package:flutter/material.dart';
import 'package:kxy_flutter_app/components/empty_widget.dart';
import 'package:kxy_flutter_app/components/loading_widget.dart';
import 'package:kxy_flutter_app/model/integral_rank.dart';
import 'package:kxy_flutter_app/view/my/integral/integral_rank_model.dart';
import 'package:kxy_flutter_app/view/my/integral/user_rank_item_widget.dart';
import 'package:provider/provider.dart';
import 'dept_rank_item_widget.dart';

class IntegralRankListWidget extends StatelessWidget {
  final String rankName;

  const IntegralRankListWidget({Key key, @required this.rankName}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Consumer<IntegralRankModel>(
      builder: (cxt, vm, child) {
        return FutureBuilder<IntegralRank>(
          future: vm.getRank(rankName),
          builder: (BuildContext context, AsyncSnapshot snapshot) {
            if (snapshot.connectionState == ConnectionState.done) {
              return body(snapshot.data, vm.rankType, context);
            }
            return LoadingWidget();
          },
        );
      },
    );
  }

  Widget body(IntegralRank rank, int rankType, BuildContext context) {
    if (rank.frontRankList.length == 0) {
      return Container(
        padding: EdgeInsets.only(top: 40),
        child: EmptyWidget(),
      );
    }

    return SingleChildScrollView(
      child: Container(
        color: Colors.white,
        child: Column(
          children: ListTile.divideTiles(
            tiles: rank.frontRankList.map((e) {
              if (rankType == 0) {
                return UserRankItemWidget(rankItem: e);
              } else {
                return DeptRankItemWidget(rankItem: e);
              }
            }).toList(),
            context: context,
            color: Colors.black12,
          ).toList(),
        ),
      ),
    );
  }
}
